#include "TwOgreStringVariable.h"

#include <sstream>

#include "TwOgreWindow.h"
#include "TwOgreWindowManager.h"

// This callbacks are needed to allow AntTweakBar to copy strings into our variable.
void TW_CALL CopyStdStringToClient(std::string& destinationClientString, const std::string& sourceLibraryString)
{
	destinationClientString = sourceLibraryString;
}

namespace TwOgre{
	StringVariable::StringVariable(Window *parent, const Ogre::String& name, bool readOnly, const Ogre::String& label, 
		const Ogre::String& value, const Ogre::String& groupName, const Ogre::String& help) :
		Variable(parent, name, TW_TYPE_STDSTRING), mValue(value)
	{
		if(readOnly)
		{
			TwAddVarRO(mParent->getTweakBar(), mName.c_str(), TW_TYPE_STDSTRING, &mValue, "");
		}
		else
		{
			TwCopyStdStringToClientFunc(CopyStdStringToClient);
			TwAddVarRW(mParent->getTweakBar(), mName.c_str(), TW_TYPE_STDSTRING, &mValue, "");
		}

		if(label.compare("") != 0)
		{
			setLabel(label);
		}
		if(groupName.compare("") != 0)
		{
			setGroup(groupName);
		}
		if(help.compare("") != 0)
		{
			setHelp(help);
		}
	}

	StringVariable::StringVariable(StringParams& params) :
        Variable(params.parent_, params.name_, params.type_), mValue(params.value_)
    {
        if((params.getCB_ != NULL) && (params.setCB_ != NULL) && (params.data_ != NULL))
        {
            TwAddVarCB(mParent->getTweakBar(), mName.c_str(), params.type_, params.setCB_, params.getCB_, params.data_, "");
        }
        else
        {
		    if(params.readOnly_)
		    {
                TwAddVarRO(mParent->getTweakBar(), mName.c_str(), params.type_, &mValue, "");
		    }
		    else
		    {
                TwAddVarRW(mParent->getTweakBar(), mName.c_str(), params.type_, &mValue, "");
		    }
        }
		
        if(params.label_.compare("") != 0)
		{
			setLabel(params.label_);
		}
		if(params.group_.compare("") != 0)
		{
			setGroup(params.group_);
		}
		if(params.help_.compare("") != 0)
		{
			setHelp(params.help_);
		}

    }
	
    StringVariable::~StringVariable()
	{
		TwRemoveVar(mParent->getTweakBar(), mName.c_str());
	}

	void StringVariable::setStringValue(const Ogre::String& value)
	{
		mValue = value;
	}
}; // End Namespace
